#!/bin/bash
root_dir=/usr/local/edh
echo "$(date +"%Y-%m-%d %H:%M:%S") INFO namenode uninstall is running..."
echo "$(date +"%Y-%m-%d %H:%M:%S") INFO checking hadoop mode..."
mode=$(cat mode)
echo "$(date +"%Y-%m-%d %H:%M:%S") INFO current mode of hadoop is $mode"
if [[ $mode == *"sa"* ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO checking hadoop processes and stopping them..."
  hdfs --daemon stop namenode
  if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping namenode success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping namenode fail"
  fi
  hdfs --daemon stop secondarynamenode
  if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping secondarynamenode success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping secondarynamenode fail"
  fi
  hdfs --daemon stop datanode
  if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping datanode success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping datanode fail"
  fi
  yarn --daemon stop resourcemanager
  if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping resourcemanager success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping resourcemanager fail"
  fi
  yarn --daemon stop nodemanager
  if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping nodemanager success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping nodemanager fail"
  fi
fi
# if [[ $mode == *"ha"* ]];then
# fi
echo "$(date +"%Y-%m-%d %H:%M:%S") INFO remove $root_dir/hadoop"
rm mode
rm -rf $root_dir/hadoop
if [[ $? -eq 0 ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO hadoop uninstall success"
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR hadoop uninstall error"
fi